package com.eascs.admin.o2o.loan.notify.common;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.notify.O2OMsgNotifyService;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.workflow.common.entity.wf.ProcessTaskMain;
import com.eascs.workflow.common.entity.wf.ProcessTemplate;
import com.eascs.workflow.common.vo.ModelResult;
import com.eascs.workflow.common.vo.WFNotifyParam;

/**
 * 工作流通知接口
 */
@Service
public class O2OWFService extends O2OWFServiceBase {
	private static Logger log = LoggerFactory.getLogger(O2OWFService.class);
	@Autowired
	private O2oLoanapplyService o2oLoanapplyService;
	
	
	public ModelResult<Boolean> wfNotify(WFNotifyParam notifyParam) {
		log.info("MQ开始处理工作流通知:WFNotifyParam[{}]", notifyParam.toString());
		try {
			if (notifyParam.getSystemModule().equals(ProcessTemplate.SYSTEM_MODULE_O2O)) {
				O2oLoanapplyEntity loanapplyEntity = o2oLoanapplyService.findById(notifyParam.getReferId());
				
				if(loanapplyEntity == null 
						&&!notifyParam.getProcessType().equals("ACCOUNT_REFUND")
						&&!notifyParam.getProcessType().equals("ACCOUNT_REMITTANCE")
						&&!notifyParam.getProcessType().equals("PROCESS_CACLE")
						&&!notifyParam.getProcessType().equals("RECOMMEND_REFUND")
						){
					log.info("工作流通知,未找到贷款单ReferId:[{}]",notifyParam.getReferId());
					return new ModelResult<Boolean>("workflow.notify.insert.error", "工作流通知,未找到贷款单");
				}
				//log.info("工作流通知,贷款单billCode:[{}]",loanapplyEntity.getBillCode());
				before(notifyParam);
				log.info("工作流通知,before()结束");
				// 流程处理中执行
				if (notifyParam.getProcessStatus() == ProcessTaskMain.PROECESS_STATUS_HANDING) {
					log.info("工作流通知,流程审核中处理");
					if (notifyParam.getProcessType().equals(ProcessTemplate.PROECESS_TYPE_BANK_REPORT)
							|| notifyParam.getProcessType().equals(ProcessTemplate.PROECESS_TYPE_BANK_REPORT_FQ)) {// 如果是处理中
						log.info("工作流通知,开始处理节点");
						return dealNodecode(notifyParam, loanapplyEntity);
					} else {
						return new ModelResult<Boolean>().withModel(Boolean.TRUE);
					}
				}
				//流程结束执行
				log.info("工作流通知,流程审核结束处理");
				O2OMsgNotifyService o2oMsgNotifyService = serviceMap.get(notifyParam.getProcessType());
				ModelResult<O2oLoanapplyEntity> motifyResult = o2oMsgNotifyService.notify(notifyParam, loanapplyEntity);
				if(motifyResult.isSuccess() && motifyResult.getModel() != null){
					loanapplyEntity = motifyResult.getModel();
				}
				log.info("工作流通知,notify方法处理成功");
				if(o2oMsgNotifyService.checkReturn()){
					log.info("工作流通知,notify方法return处理");
					return new ModelResult<Boolean>().withModel(Boolean.TRUE);
				}
				after(notifyParam,loanapplyEntity);
				log.info("工作流通知,after()结束");
			}
		} catch (Exception e) {
			log.error("ReferId:[{}],工作流消息异常:[{}]", notifyParam.getReferId(), e);
			return new ModelResult<>("workflow.notify.insert.error", "流程通知插入失败");
		}
		log.info("结束处理工作流通知");
		return new ModelResult<Boolean>().withModel(Boolean.TRUE);
	}

	
	
}
